package wortubung.quiz;

import wortubung.WortUbung;
import wortubung.wordlist.WordQuery;

import static wortubung.util.CommonUtils.*;

public class QuizGenOptions extends WordQuery {
    public static final int DEFAULT_MULTI_CHOICE_SIZE = 4;
    public static final int DEFAULT_CROSS_MATCH_SIZE = 10;
    public static final int DEFAULT_TOTAL_TESTS = 80;
    public static final int DEFAULT_TOTAL_INTERACTIVE_TESTS = 20;

    public enum QuizType {
		MULTI_CHOICE,
		CROSS_MATCH
	};

    private boolean isInteractive;
    private boolean instant;
    private QuizType type = QuizType.MULTI_CHOICE;
    private int suiteSize = -1;
    private String source = null;

	public QuizGenOptions(WortUbung dictionary, String params, boolean isInteractive) {
		super(dictionary, params);
    	if (isHelp())
    		return;

    	this.isInteractive = isInteractive;
		instant = false;
		if (isInteractive)
			total = DEFAULT_TOTAL_INTERACTIVE_TESTS;
		else
			total = DEFAULT_TOTAL_TESTS;

		for (String s : params.split(" +")) {
			String[] keyVal = s.split("=");
			if (keyVal.length == 1) {
				if (s.trim().equalsIgnoreCase("multichoice"))
					type = QuizType.MULTI_CHOICE;
				else if (s.trim().equalsIgnoreCase("crossmatch"))
					type = QuizType.CROSS_MATCH;
				else if (s.trim().equalsIgnoreCase("instant"))
					instant = true;
			}
			else if (keyVal[0].equals("size"))
				suiteSize = Integer.parseInt(keyVal[1]);
			else if (keyVal[0].equals("instant"))
				instant = checkBoolean(keyVal[1]);
			else if (keyVal[0].equals("total")) {
				if ("all".equalsIgnoreCase(keyVal[1]))
					total = Integer.MAX_VALUE;
				else
					total = Integer.parseInt(keyVal[1]);
			}
		}
	}

	public boolean isInteractive() { return isInteractive; }
	public boolean isInstantAnswer() { return instant; }
	public boolean isMultiChoice() { return type == QuizType.MULTI_CHOICE; }
	public boolean isCrossMatch() { return type == QuizType.CROSS_MATCH; }
	public QuizType getType() { return type; }
	public int getTotal() { return total; }
	public String getSource() { return source; }
	public int getSuiteSize() { return (suiteSize > 0) ? suiteSize : isMultiChoice() ? DEFAULT_MULTI_CHOICE_SIZE : DEFAULT_CROSS_MATCH_SIZE; }
	public boolean exceedsTotal(int totalTests) { return (total > 0) && (totalTests >= total); }

	public void showHelp(String invalidOptionToWarn) {
		final String commonHelpText =
	             "    multichoice                    Generate multi-choice tests (default).\n" +
	             "    crossmatch                     Generate cross-match tests.\n" +
	             "    size=<number>                  The size of a multi-choice question (default: " + DEFAULT_MULTI_CHOICE_SIZE + ") or a cross-match suite (default: " + DEFAULT_CROSS_MATCH_SIZE + ").\n";

		if (isInteractive()) {
    		if (invalidOptionToWarn != null)
    			promptln("Invalid /quiz option of '" + invalidOptionToWarn + "'.\n");
    		promptln("  The /quiz command can take a number of the following options, and source is required:\n" + commonHelpText +
    		         "    total=<number>|all             The maximum number of words in the test. Default: " + DEFAULT_TOTAL_INTERACTIVE_TESTS + ".\n" +
    		         "    instant[=yes|no]               Instantly show correctness of answer. Without value, it is yes. Default: no.\n" +
    		         super.getHelpText());
		} else {
			if (invalidOptionToWarn != null)
				promptln("Invalid /genquiz option of '" + invalidOptionToWarn + "'.\n");
			promptln("  The /genquiz command can take a number of the following options, and source is required:\n" + commonHelpText +
			         "    total=<number>|all             The maximum number of words in the test. Default: " + DEFAULT_TOTAL_TESTS + ".\n" +
			         "    out=<output_file>              The name of the output file. Default: none.\n" +
    		         super.getHelpText());
		}
	}
}
